package com.hzsai.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * hzsai 2020/1/13
 * usage:
 **/
public class MyAnnotationTest {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, NoSuchFieldException, InvocationTargetException {

        DodoBird bi = new DodoBird();

        Class<?> c = DodoBird.class;

        Field ageField = c.getDeclaredField("age");
        System.out.println(ageField.getInt(bi));

        Method methodField = c.getDeclaredMethod("say");
        methodField.setAccessible(true);
        System.out.println(methodField.invoke(bi, null));

        Class<?> cc = Dodo.class;

        if (cc.getAnnotation(MyAnnotation.class) != null) {
            System.out.println("AnnoPresentation");
            Field[] fields = cc.getDeclaredFields();
            Method[] methods = cc.getDeclaredMethods();
            System.out.println("Anno size: " + fields.length);
            System.out.println("Method size: " + methods.length);
            for (Field field : fields) {
                Annotation[] annotations = field.getDeclaredAnnotations();
                for (Annotation anno : annotations) {
                    System.out.println("Field anno: "  + anno);
                }
            }

            for (Method method : methods) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                for (Annotation anno : annotations) {
                    System.out.println("Method anno: " + anno);
                }
            }
//            Field _age = cc.getDeclaredField("age");
//            System.out.println(_age.getAnnotatedType().getType());
        }
    }
}
